using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WeatherSystem.API.Services;

namespace WeatherSystem.API.Controllers
{
    /// <summary>
    /// 数据导出控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [Authorize(Policy = "UserOrAdmin")]
    public class ExportController : ControllerBase
    {
        private readonly IExportService _exportService;
        private readonly ILogger<ExportController> _logger;

        public ExportController(IExportService exportService, ILogger<ExportController> logger)
        {
            _exportService = exportService;
            _logger = logger;
        }

        /// <summary>
        /// 导出天气数据为Excel格式
        /// </summary>
        [HttpGet("weather/excel")]
        public async Task<IActionResult> ExportWeatherDataToExcel(
            [FromQuery] string? cityId = null,
            [FromQuery] string? startDate = null,
            [FromQuery] string? endDate = null)
        {
            try
            {
                var data = await _exportService.ExportWeatherDataToExcelAsync(cityId, startDate, endDate);
                var fileName = $"weather_data_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx";

                return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出天气数据Excel失败");
                return StatusCode(500, new { message = "导出失败", error = ex.Message });
            }
        }

        /// <summary>
        /// 导出天气数据为CSV格式
        /// </summary>
        [HttpGet("weather/csv")]
        public async Task<IActionResult> ExportWeatherDataToCsv(
            [FromQuery] string? cityId = null,
            [FromQuery] string? startDate = null,
            [FromQuery] string? endDate = null)
        {
            try
            {
                var data = await _exportService.ExportWeatherDataToCsvAsync(cityId, startDate, endDate);
                var fileName = $"weather_data_{DateTime.Now:yyyyMMdd_HHmmss}.csv";

                return File(data, "text/csv", fileName);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出天气数据CSV失败");
                return StatusCode(500, new { message = "导出失败", error = ex.Message });
            }
        }

        /// <summary>
        /// 导出统计数据为Excel格式
        /// </summary>
        [HttpGet("statistics/excel")]
        public async Task<IActionResult> ExportStatisticsToExcel(
            [FromQuery] string? cityId = null,
            [FromQuery] string? startDate = null,
            [FromQuery] string? endDate = null)
        {
            try
            {
                var data = await _exportService.ExportStatisticsToExcelAsync(cityId, startDate, endDate);
                var fileName = $"statistics_data_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx";

                return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出统计数据Excel失败");
                return StatusCode(500, new { message = "导出失败", error = ex.Message });
            }
        }

        /// <summary>
        /// 导出城市数据为Excel格式
        /// </summary>
        [HttpGet("cities/excel")]
        public async Task<IActionResult> ExportCitiesToExcel()
        {
            try
            {
                var data = await _exportService.ExportCitiesToExcelAsync();
                var fileName = $"cities_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx";
                
                return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出城市数据Excel失败");
                return StatusCode(500, new { message = "导出失败", error = ex.Message });
            }
        }
    }
}